/* 引入less */
/* 重置样式 */
require('../css/commCss/normalize.css')
/* 重置样式补充 */
require('../css/commCss/reset.css')
/* 公共样式 */
require('../css/commCss/property.css')
/* 公共属性 */
require('../css/commCss/basic.less')
/* 字体图标 */
require('../fonts/iconfont.css')
/* 当前页面 */
require('../css/login.less')





document.addEventListener('DOMContentLoaded', function () {
            /**
             * 获取 定义数据
             */

            let captchaText = ''
            const CaptchaMini = require("captcha-mini") //初始化验证码
            const axios = require('axios') //初始化axios
            /* console.log(axios); */
            //weui-插件库
            require('weui')
            const weui = require('weui.js')
            /**
             * 
             * 0. 获取元素
             */

            const account = document.querySelector('#account') //用户名
            const captcha = document.querySelector('#captcha') //验证码
            const password = document.querySelector('#password') //密码
            const checkPassword = document.querySelector('#checkPassword') //确认密码
            const submitBtn = document.querySelector('#submitBtn') //注册按钮
            const erronMsg = document.querySelector('#erronMsg') //错误提醒
            /**
             * 1. 需求 点击注册按钮 进行验证
             */

            submitBtn.addEventListener('click', function () {
                    //console.log(1);
                    //获取每个输入框的value
                    const accountVal = account.value; //用户名
                    const captchaVal = captcha.value; //验证码
                    const passwordVal = password.value; //密码
                    const checkPasswordVal = checkPassword.value; //确认密码

                    //console.log(accountVal,captchaVal,passwordVal,checkPasswordVal);

                    //利用正则表达式判断

                    //手机号正则
                    const accountReg = /^1[3-9][0-9]{9}$/
                    //密码正则
                    const passwordReg = /^[a-zA-Z]\w{5,11}$/
                    if (!accountReg.test(accountVal)) { //手机号判断
                        erronMsg.textContent = '手机号错误'
                        return;
                    } else if (!captchaVal === captchaText) {
                        erronMsg.textContent = '验证码错误'
                        return;
                    } else if (!passwordReg.test(passwordVal)) { //密码判断
                        erronMsg.textContent = '密码错误'
                        return;
                    } else if (passwordVal != checkPasswordVal) { //再次确认密码
                        erronMsg.textContent = '两次输入密码不一致'
                        return;
                    } else { //全部正确
                        //清空错误提示消息
                        erronMsg.textContent = ''
                        //发送请求

                        let url = 'http://139.9.177.51:8099/users/add';
                        axios.post(url, {
                            account: accountVal,
                            password: passwordVal
                        }).then(function (res) {
                                //判断
                                if (res.data.status === 0) {
                                    
                                    //弹出轻提示框
                                    weui.toast('注册成功', {
                                            duration: 1000,
                                            //className: 'custom-classname', //自定义类名
                                            callback: function () { //回调函数
                                                //页面跳转
                                                location.href = './register.html'
                                            }
                                        })
                                    }else{
                                        erronMsg.textContent = `注册失败`
                                    }
                                })
                        }

                    })

                /**
                 * 2. 验证码
                 */
                let captchaObj = new CaptchaMini({
                    lineWidth: 1, //线条宽度
                    lineNum: 4, //线条数量
                    dotR: 2, //点的半径
                    dotNum: 25, //点的数量
                    preGroundColor: [10, 80], //前景色区间
                    backGroundColor: [150, 250], //背景色区间
                    fontSize: 46, //字体大小
                    fontFamily: ['Georgia', '微软雅黑', 'Helvetica', 'Arial'], //字体类型
                    fontStyle: 'fill', //字体绘制方法，有fill和stroke
                    content: 'abcdefghijklmnopqrstuvw1234567890', //验证码内容
                    length: 4 //验证码长度
                }); captchaObj.draw(document.querySelector('#captcha1'), function (r) {
                    captchaText = r
                });
                //console.log(captchaText);
            })